package handle

import (
	"fmt"

	"github.com/labstack/echo/v4"
)

type Req struct {
}

func (r Req) InitRoute(party *echo.Group) {
	p := party.Group("/req")
	p.POST("/form-data", r.form())
	p.POST("/form-ulencoded", r.formUrlencoded())

	p.GET("/path/:name/:ext", r.path())
	p.GET("/query", r.query())
	p.POST("/binary", r.binary())
	p.POST("/raw", r.raw())
}
func (r Req) form() echo.HandlerFunc {
	return func(ctx echo.Context) error {
		form, err := ctx.MultipartForm()
		if err != nil {
			return ctx.JSON(402, err)
		}
		for s, strings := range form.Value {
			fmt.Println("MultipartForm.Value", s, strings)
		}
		fmt.Println(ctx.FormParams())
		return nil
	}
}
func (r Req) formUrlencoded() echo.HandlerFunc {
	return func(ctx echo.Context) error {
		fmt.Println(ctx.FormParams())
		return nil
		//fmt.Println(ctx.PostForm("name"))
	}
}
func (r Req) query() echo.HandlerFunc {
	return func(ctx echo.Context) error {
		fmt.Println(ctx.QueryParams())
		fmt.Println(ctx.QueryString())
		return nil
	}
}
func (r Req) path() echo.HandlerFunc {
	return func(ctx echo.Context) error {
		fmt.Println(ctx.Path())
		fmt.Println(ctx.ParamNames())
		return nil
	}
}
func (r Req) binary() echo.HandlerFunc {
	return func(ctx echo.Context) error {
		return nil
	}
}
func (r Req) raw() echo.HandlerFunc {
	return func(ctx echo.Context) error {
		response := ctx.Response()
		bytes := make([]byte, response.Size)
		write, err := response.Write(bytes)
		if err != nil {
			return err
		}
		return ctx.JSON(200, write)
	}
}
